import {
	ONLINEHOST
} from '@/config/index.js'
import {
	loginTimeOut,
	toast
} from '@/tools/index.js'
import store from '@/store/index'
//  // 默认值
const request = function(url, params, loading = false, loadText='加载中...', header, method = 'POST', host = "java") {
	if (loading) uni.showLoading({
		title: loadText,
		mask:true
	})
	let headers = {
		'content-type': 'application/json', // 默认值
		'token': store.state.token?store.state.token:'',
	}
	// #ifdef MP-TOUTIAO
	let appid = 'tte9969f2b76acab1d'
	// #endif
	// #ifdef MP-ALIPAY
	let appid = '2021004137671427'
	// #endif
	let _params = {...params,...{appid:appid,customerId :uni.getStorageSync('openid')?uni.getStorageSync('openid'):''}}
	if(header){
		headers = Object.assign(headers,header)
	}
	let _host =ONLINEHOST
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: _params,
			method: method,
			header: headers,
			success(res) {
				// console.log(res)
				if (loading) uni.hideLoading()
				if (res.data.code === 0) {
					resolve(res.data);
				} else if (res.data.code == 400) {
					loginTimeOut()
				} else {
					uni.showToast({
						title:res.data.msg,
						icon:'none'
					})
					resolve(res.data)
				}
			},
			fail: function(res) {
				// toast('连接异常，请重试')
				resolve({
					status: 500,
					msg: '连接异常，请重试'
				})
			},
			complete: function(res) {}
		})
	})
}





/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params, loading,loadText, header, host) {
	return request(url, params, loading,loadText, header, 'POST', host)
}

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params, loading,loadText, header, host) {
	return request(url, params, loading,loadText, header, 'GET', host)
}
